package cn.telling.telMarketCentre.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import cn.telling.common.util.TellingConstance;
import cn.telling.product.vo.ShopProductVo;
import cn.telling.telMarketCentre.dao.ITempsalerClearProductListDao;
import cn.telling.utils.StringHelperTools;

@Repository("tempsalerClearProductListDao")
public class TempsalerClearProductListDaoImpl implements ITempsalerClearProductListDao {
	private static Logger logger = Logger.getLogger(TempsalerClearProductListDaoImpl.class);
	
	@Autowired
	@Qualifier("film-template")
	private JdbcTemplate jdbcTemplate;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public List<Map<String, String>> loadTempsalerClearProductList(String choose,String supplyId) {
		String sql = "select pm.productId,pm.productName, pb.brandName " +
					 " from product_main pm,product_brand pb,tempsaler_clear_product_list tcp " +
					 " where pb.brandId = pm.brandId " +
					 " and tcp.productId = pm.productId " +
					 " and pm.saleType = '" + TellingConstance.SALETYPE_2 + "' " + 
					 " and pm.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' " +
					 " and pm.publish = '1' " +
					 " and pm.productname like '%" + choose + "%' " +
					 " and tcp.sellerId = " + supplyId +
					 " order by pb.brandName, pm.productName";
		logger.debug("查询临时卖家类产品列表:" + sql);
		List<Map<String, String>> list = jdbcTemplate.query(sql, rowMapper);
		return list;
	}
	
	private ParameterizedRowMapper<Map<String, String>> rowMapper = new ParameterizedRowMapper<Map<String,String>>() {
		@Override
		public Map<String, String> mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			Map<String, String> map = new HashMap<String, String>();
			map.put("productId", StringHelperTools.nvl(rs.getString("productId")));
			map.put("productName", StringHelperTools.nvl(rs.getString("productName")));
			map.put("brandName", StringHelperTools.nvl(rs.getString("brandName")));
			map.put("checked", "0");// 默认未选中
			map.put("topFlag", "0");//默认不在top值范围内
			return map;
		}
	};
	
	/**
	 * 查询临时卖家 产品已上架、已下架产品
	 */
	@Override
	public List<ShopProductVo> getShopProductListNoPage(String userId) {
		String sql = "select sp.id, sp.productId,sp.shopId,sp.supplyId,pm.productName,pp.picturePath1,max(sa.saleType) as saleMode " +
				 " from shop_product sp " +
				 " inner join product_main pm on sp.productId = pm.productId and pm.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' " +
				 " left  join product_pic_relation ppr on pm.productId = ppr.product_id " + 
				 " left  join product_picture pp on ppr.pictureId = pp.pictureId and pp.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' " +
				 " inner join tempsaler t on sp.supplyId = t.id " +
				 " inner join supply_area0 sa on sp.id = sa.supplyProductId and sa.del_flag = '" + TellingConstance.DEL_FLAG_1+"' and sa.isValId = '" + TellingConstance.IS_VALID_01 + "' " +
				 " where t.userId = '" + userId + "' and sa.saleStatus <> '1' " +
				 " and sp.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' and sp.isValId = '" + TellingConstance.IS_VALID_01 + "' and pm.saleType = '" + TellingConstance.SALETYPE_2 + "' " +
				 " group by sp.id, sp.productId,sp.shopId,sp.supplyId,pm.productName,pp.picturePath1 " +
				 " order by sp.id desc ";
		return jdbcTemplate.query(sql, tempMapper);
	}
	
	private ParameterizedRowMapper<ShopProductVo> tempMapper = new ParameterizedRowMapper<ShopProductVo>() {
		@Override
		public ShopProductVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			ShopProductVo shopProductVo = new ShopProductVo();
			shopProductVo.setId(rs.getBigDecimal("id"));
			shopProductVo.setProductId(rs.getBigDecimal("productId"));
			shopProductVo.setShopId(rs.getString("shopId"));
			shopProductVo.setSupplyId(rs.getBigDecimal("supplyId"));
			shopProductVo.setProductName(rs.getString("productName"));
			shopProductVo.setProductPic(rs.getString("picturePath1"));
			shopProductVo.setSaleMode(rs.getString("saleMode"));
			return shopProductVo;
		}
	};

}
